/**
 * jims
 */
package com.jims.master.modules.drug.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.drug.entity.*;
import com.jims.master.modules.drug.service.DrugDetailsService;
import com.jims.master.modules.drug.service.DrugExportService;
import com.jims.master.modules.drug.service.DrugImportDetailService;
import com.jims.master.modules.drug.service.DrugInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 药理信息Controller
 * @author DT
 * @version 2017-03-15
 */
@Controller
@RequestMapping(value = "${adminPath}/drug/drugDetails")
public class DrugDetailsController extends BaseController {

    @Autowired
    private DrugDetailsService drugDetailsService;
    @Autowired
    private DrugExportService drugExportService;
    @Autowired
    private DrugInfoService drugInfoService;
    @Autowired
    private DrugImportDetailService drugImportDetailService;

    @ModelAttribute
    public DrugDetails get(@RequestParam(required=false) String id) {
        DrugDetails entity = null;
        if (StringUtils.isNotBlank(id)){
            entity = drugDetailsService.get(id);
        }
        if (entity == null){
            entity = new DrugDetails();
        }
        return entity;
    }

    /**
     * 根据masterId查询出库详情
     * @param drugExportDetail
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"exportdetail", ""})
    public String listexportdetail(DrugExportDetail drugExportDetail,HttpServletRequest request, HttpServletResponse response,  Model model) {
        List<DrugExportDetail> drugExportDetaiList= drugExportService.findBymasterId(drugExportDetail);
        model.addAttribute("drugExportDetaiList", drugExportDetaiList);
        model.addAttribute("drugExportDetail", drugExportDetail);
        return "modules/drug/drugExportDetailsIndex";
    }


    /**
     * 根据masterId查询出库详情
     * @param drugExportDetail
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"findByMaster", ""})
    public String findByMaster(DrugExportDetail drugExportDetail,HttpServletRequest request, HttpServletResponse response,  Model model) {
        List<DrugExportDetail> drugExportDetaiList= drugExportService.findBymasterId(drugExportDetail);
        model.addAttribute("drugExportDetaiList", drugExportDetaiList);
        model.addAttribute("drugExportDetail", drugExportDetail);
        DrugImportMaster drugImportMaster = new DrugImportMaster();
        model.addAttribute("drugImportMaster", drugImportMaster);
        return "modules/drug/drugImportBatchForm";
    }

    /**
     * 根据masterId查询入库详情
     * @param drugImportDetail
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"importdetail", ""})
    public String listimportdetail(DrugImportDetail drugImportDetail,HttpServletRequest request, HttpServletResponse response,  Model model) {
        List<DrugImportDetail> drugImportDetailList= drugImportDetailService.findBymasterId(drugImportDetail);
        model.addAttribute("drugImportDetailList", drugImportDetailList);
        model.addAttribute("drugImportDetail", drugImportDetail);
        return "modules/drug/drugImportDetailsIndex";
    }
    /**
     * 出库单据查询
     * @param drugExportDetail
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"exportQue", ""})
    public String listExportDetail(DrugExportDetail drugExportDetail,HttpServletRequest request, HttpServletResponse response,  Model model) {
        Page<DrugExportDetail> page= drugExportService.findDetailList(new Page<DrugExportDetail>(request, response), drugExportDetail);
        model.addAttribute("page", page);
        return "modules/drug/drugExportDeatailsIndex";
    }

	/**
	 * 入库单据查询
	 * @param drugImportDetail
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"importQue", ""})
	public String listImportDetail(DrugImportDetail drugImportDetail,HttpServletRequest request, HttpServletResponse response,  Model model) {
		Page<DrugImportDetail> page= drugImportDetailService.findDetailsList(new Page<DrugImportDetail>(request, response), drugImportDetail);
		model.addAttribute("page", page);
		return "modules/drug/drugImportDeatailsIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(DrugDetails drugDetails, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DrugDetails> page = drugDetailsService.findPage(new Page<DrugDetails>(request, response), drugDetails);
		model.addAttribute("page", page);
		return "modules/drug/drugDetailsIndex";
	}

	/**
	 * 药理信息添加并获取药品信息
	 * @param drugDetails
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(DrugDetails drugDetails, HttpServletRequest request, HttpServletResponse response, Model model) {
		DrugInfo drugInfo=new DrugInfo();
		Page<DrugInfo> page=drugInfoService.findPage(new Page<DrugInfo>(request,response),drugInfo);
		model.addAttribute("page",page);
		model.addAttribute("drugDetails", drugDetails);
		return "modules/drug/drugDetailsForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(DrugDetails drugDetails, Model model) {
	    StringData data=new StringData();

			int i=drugDetailsService.save(drugDetails);
			if(i>0){
				data.setCode("success");
				data.setData("保存成功");
			}else{
				data.setCode("error");
				data.setData("保存失败");
			}
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(DrugDetails drugDetails) {
	    StringData data=new StringData();
		int i=drugDetailsService.delete(drugDetails);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
        return data;
    }
    /**
     * 药品入库单据打印
     * @param drugImportDetail
     * @return com.jims.common.data.StringData
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "drugImportDetailsPrint")
    @ResponseBody
    public StringData drugImportDetailsPrint(DrugImportDetail drugImportDetail){
        StringData stringData = new StringData();
        String printPath = drugImportDetailService.drugImportDetailsPrint(drugImportDetail);
        stringData.setCode("success");
        stringData.setData(printPath);
        return stringData;
    }
    /**
     * 药品出库单据打印
     * @param drugExportDetail
     * @return com.jims.common.data.StringData
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "drugExportDetailsPrint")
    @ResponseBody
    public StringData drugExportDetailsPrint(DrugExportDetail drugExportDetail){
        StringData stringData = new StringData();
        String printPath = drugExportService.drugExportDetailsPrint(drugExportDetail);
        stringData.setCode("success");
        stringData.setData(printPath);
        return stringData;
    }
}